package com.study.studyroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.studyroom.entity.bo.OptionBo;
import com.study.studyroom.entity.bo.RoomBo;
import com.study.studyroom.entity.domain.Room;
import com.study.studyroom.entity.domain.RoomType;
import com.study.studyroom.entity.enums.UnitTypeEnum;
import com.study.studyroom.entity.vo.Condition;
import com.study.studyroom.entity.vo.room.RoomVo;
import com.study.studyroom.mapper.RoomMapper;
import com.study.studyroom.mapper.RoomTypeMapper;
import com.study.studyroom.service.RoomService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@AllArgsConstructor
public class RoomServiceImpl  extends BaseService implements RoomService {

    private final RoomMapper roomMapper;
    private final RoomTypeMapper roomTypeMapper;
    @Override
    public IPage<RoomBo> getRoom(Condition<RoomVo> condition) {
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Room::getDeleteFlag,false);
        if (condition.getQueryInfo()!=null){
            RoomVo queryInfo = condition.getQueryInfo();
            if (StringUtils.isNotBlank(queryInfo.getRoomNo())){
                queryWrapper.like(Room::getRoomNo,queryInfo.getRoomNo());
            }
            if (StringUtils.isNotBlank(queryInfo.getRoomUrl())){
                queryWrapper.like(Room::getRoomUrl,queryInfo.getRoomUrl());
            }
            if (queryInfo.getDataStatus()!=null){
                queryWrapper.eq(Room::getDataStatus,queryInfo.getDataStatus());
            }
            if (queryInfo.getRoomType()!=null){
                queryWrapper.eq(Room::getRoomType,queryInfo.getRoomType());
            }
        }
        Page<Room> roomPage = roomMapper.selectPage(Page.of(condition.getPageIndex(), condition.getPageSize()), queryWrapper);
        Set<Integer> roomTypeIds = new HashSet<>();
        roomPage.getRecords().forEach(item->roomTypeIds.add(item.getRoomType()));
        List<RoomType> roomTypes = this.selectByIdList(roomTypeMapper, roomTypeIds);
        Map<Integer,RoomType> mapping = new HashMap<>();
        for (RoomType roomType : roomTypes) {
            mapping.put(roomType.getId(),roomType);
        }
        Page<RoomBo> result = new Page<>();
        result.setTotal(roomPage.getTotal());
        result.setRecords(new ArrayList<>());
        for (Room record : roomPage.getRecords()) {
            RoomBo roomBo = new RoomBo();
            RoomType roomType = mapping.get(record.getRoomType());
            BeanUtils.copyProperties(record,roomBo);
            if (roomType!=null){
                roomBo.setStartPrice(roomType.getStartPrice());
                roomBo.setUnitType(UnitTypeEnum.getByCode(roomType.getUnitType()).getDesc());
                roomBo.setUnitPrice(roomType.getUnitPrice());
                roomBo.setMaxPrice(roomType.getMaxPrice());
            }
            result.getRecords().add(roomBo);
        }
        return result;
    }

    @Override
    public Integer addRoom(RoomVo roomVo) {
        Room room = new Room();
        BeanUtils.copyProperties(roomVo,room);
        room.setId((int) System.currentTimeMillis());
        return this.insert(roomMapper,room);
    }

    @Override
    public RoomBo updateRoom(RoomVo roomVo) {
        Room room = this.selectById(roomMapper, roomVo.getId());
        Room newRoom = new Room();
        BeanUtils.copyProperties(roomVo,newRoom);
        room = this.update(roomMapper,newRoom,room);
        RoomBo roomBo = new RoomBo();
        BeanUtils.copyProperties(room,roomBo);
        return roomBo;
    }

    @Override
    public RoomBo updateRoomStatus(Integer id) {
        Room room = this.selectById(roomMapper,id);
        room=this.updateDataStatusById(roomMapper,room);
        RoomBo roomBo = new RoomBo();
        BeanUtils.copyProperties(room,roomBo);
        return roomBo;

    }

    @Override
    public Integer deleteRoom(Integer id) {
        Room room = roomMapper.selectById(id);
        return this.deleteById(roomMapper,room);
    }

    @Override
    public List<OptionBo> getRoomByName(String name) {
        LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            roomLambdaQueryWrapper.like(Room::getRoomNo,name);
        }
        List<Room> rooms = roomMapper.selectList(roomLambdaQueryWrapper);
        List<OptionBo> result = new ArrayList<>();
        for (Room room : rooms) {
            result.add(new OptionBo(room.getId(),room.getRoomNo()));
        }
        return result;
    }
}
